package com.servicelayer.project;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.core.protocols.IHibernateParentEnumeratorDelegate;
import com.core.protocols.IKVObject;
import com.core.protocols.IServiceLayerDelegate;
import com.managers.hibernate.ExceptionHandler;
import com.managers.hibernate.SessionFactoryManager;
import com.managers.hibernate.exceptionevent.ExceptionEvent;
import com.model.project.ProjectItem;
import com.model.project.Version;
import com.servicelayer.base.HibernateServiceLayer;


public class VersionListLayer
        extends HibernateServiceLayer {

    public Class <? extends IKVObject> entityClass () {

        return Version.class;
    }



    public void enumerateParentObjectsWithDelegate ( IKVObject kvObject,
                                                     IHibernateParentEnumeratorDelegate delegate ) {

        if ( kvObject == null ) {
            return;
        }
        Version projectVersion = (Version) kvObject;
        delegate.onEnumarateParent(projectVersion);

        ProjectItem projectItem = projectVersion.getParentProject();
        delegate.onEnumarateParent(projectItem);
    }



    public IKVObject addProjectVersion ( IKVObject template,
                                         Integer parentProjectId ) {


        IKVObject result = null;

        /**
         * check 4 template
         */

        if ( template == null ) {
            return result;
        }


        IServiceLayerDelegate delegate = this.getLayerDelegate();


        Session session = SessionFactoryManager.sharedInstance().openSession();
        /**
         * check 4 session
         */
        if ( session == null ) {
            return result;
        }

        ProjectListLayer projectListLayer = new ProjectListLayer();
        ProjectItem projectItem = (ProjectItem) projectListLayer.objectByUNID(parentProjectId, session, null);

        /**
         * check 4 parent item
         */
        if ( projectItem == null ) {
            if ( delegate != null ) {
                delegate.responseResponseItemsList(null, this, new Exception("Project not found"));
            }
            return null;
        }



        /**
         * create an entity
         */

        Exception exception = null;
        try {

            result = VersionListLayer.createEntityWithTemplate(template, null, session);

        } catch ( Exception e ) {
            result = null;
        }


        if ( result == null ) {
            return result;
        }

        /**
         * add to parent list
         */


        Transaction transaction = null;
        try {

            Version version = (Version) result;
            transaction = session.beginTransaction();
            version.setParentProject(projectItem);
            projectItem.getProjectVersions().add(version);
            transaction.commit();

        } catch ( Exception e1 ) {
            ExceptionHandler.handleException(e1, new ExceptionEvent(ExceptionEvent.ADDCHILDOBJECT));
            result = null;
        }

        List <IKVObject> list = null;
        if ( result != null ) {
            list = new ArrayList <IKVObject>();
            list.add(result);
        }

        /**
         * send result to delegate
         */

        if ( delegate != null ) {
            delegate.responseResponseItemsList(list, this, exception);
        }



        return result;
    }
}
